cba1c0602bf69cec839d06351ccb92b41d1e7f64,src/com/opencms/flex/cache/CmsFlexRequestDispatcher.java,CmsFlexRequestDispatcher,include,#ServletRequest#ServletResponse#,138

Before Change


        if ((m_extTarget == null) && (controller != null)) {
            // Check if the file exists in the VFS, if not set external target
            try {
                cms.readFileHeader(m_vfsTarget);
            } catch (CmsException e) {
                if (e.getType() == CmsException.C_NOT_FOUND) {
                    // File not found in VFS, treat it as external file

After Change


        if ((m_extTarget == null) && (controller != null)) {
            // Check if the file exists in the VFS, if not set external target
            try {
                resource = cms.readFileHeader(m_vfsTarget);
            } catch (CmsException e) {
                if (e.getType() == CmsException.C_NOT_FOUND) {
                    // File not found in VFS, treat it as external file
                    m_extTarget = m_vfsTarget;
                }
            }
        }
                
        if ((m_extTarget != null) || (controller == null)) {
            includeExternal(req, res);
            return;
        }
        
        CmsFlexCache cache = controller.getCmsCache();
        
        // this is a request through the CMS
        CmsFlexRequest f_req = controller.getCurrentRequest();
        CmsFlexResponse f_res = controller.getCurrentResponse();
        
        if (f_req.containsIncludeCall(m_vfsTarget)) {
            // This resource was already included earlier, so we have a (probably endless) inclusion loop
            throw new ServletException("FlexDispatcher: Dectected inclusion loop for target " + m_vfsTarget);
        } else {     
            f_req.addInlucdeCall(m_vfsTarget);
        }
       
        // Do nothing if response is already finished (probably as a result of an earlier redirect)
        if (f_res.isSuspended()) return;
        
        // Indicate to response that all further output or headers are result of include calls
        f_res.setCmsIncludeMode(true);
                
        // Create wrapper for request & response
        CmsFlexRequest w_req = new CmsFlexRequest((HttpServletRequest)req, controller, m_vfsTarget);
        CmsFlexResponse w_res = new CmsFlexResponse((HttpServletResponse)res, controller); 
        
        // Push req/res to controller queue
        controller.pushRequest(w_req);
        controller.pushResponse(w_res);             
        
        CmsFlexCacheEntry entry = null;
        if (f_req.isCacheable()) {
            // Caching is on, check if requested resource is already in cache            
            entry = cache.get(w_req.getCmsCacheKey());
            if (entry != null) {
                // The target is already in the cache
                try {
                    if (DEBUG > 0) System.err.println("FlexDispatcher: Loading file from cache for " + m_vfsTarget);
                    entry.service(w_req, w_res);
                } catch (com.opencms.core.CmsException e) {
                    throw new ServletException("FlexDispatcher: Error while loading file from cache for " + m_vfsTarget + "\n" + e, e);
                }                       
            } else { 
                // Cache is on and resource is not yet cached, so we need to read the cache key for the response
                CmsFlexCacheKey res_key = cache.getKey(CmsFlexCacheKey.getKeyName(m_vfsTarget, w_req.isOnline(), w_req.isWorkplace()));            
                if (res_key != null) {
                    // Key already in cache, reuse it
                    w_res.setCmsCacheKey(res_key);                                             
                } else {                                
                    // Cache key is unknown, read key from properties
                    String cacheProperty = null;
                    try {
                        // Read caching property from requested VFS resource                                     
                        cacheProperty = cms.readProperty(m_vfsTarget, org.opencms.loader.I_CmsResourceLoader.C_LOADER_CACHEPROPERTY);                    
                        cache.putKey(w_res.setCmsCacheKey(cms.getRequestContext().addSiteRoot(m_vfsTarget), cacheProperty, f_req.isOnline(), f_req.isWorkplace()));                                            
                    } catch (com.opencms.core.CmsException e) {
                        if (e.getType() == CmsException.C_FLEX_CACHE) {
                            // Invalid key is ignored but logged, used key is cache=never
                            if (OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_INFO)) 
                                OpenCms.log(I_CmsLogChannels.C_OPENCMS_INFO, "[FlexCache] Invalid cache key for external resource \"" + m_vfsTarget + "\": " + cacheProperty);
                            // There will be a vaild key in the response ("cache=never") even after an exception
                            cache.putKey(w_res.getCmsCacheKey());
                        } else {
                            // All other errors are not handled here
                            throw new ServletException("FlexDispatcher: Error while loading cache properties for " + m_vfsTarget + "\n" + e, e);
                        }
                    }                
                    if (DEBUG > 1) System.err.println("FlexDispatcher: Cache properties for file " + m_vfsTarget + " are: " + cacheProperty);
                }
            }
        }

        if (entry == null) {
            // The target is not cached (or caching off), so load it with the internal resource loader
            org.opencms.loader.I_CmsResourceLoader loader = null;

            String variation = null;
            // Check cache keys to see if the result can be cached 
            if (w_req.isCacheable()) variation = w_res.getCmsCacheKey().matchRequestKey(w_req.getCmsCacheKey());
            // Indicate to the response if caching is not required
            w_res.setCmsCachingRequired(variation != null);
                        
            try {
                if (resource == null) resource = cms.readFileHeader(m_vfsTarget);
                int type = resource.getLoaderId();
                if (DEBUG > 0) System.err.println("FlexDispatcher: Loading resource type " + type);
                loader = OpenCms.getLoaderManager().getLoader(type);